package org.hs.phd.common.charting;

import java.awt.Color;
import java.util.List;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.StandardChartTheme;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class LineChartDrawer{
	
    private XYSeriesCollection series;
    private int xCount = 1;
	private final String title;
	private final String xAxisLabel;
	private final String yAxisLAbel;
	private JFreeChart chart;
	private final List<? extends SeriesInfo> seriesInfo;
	private final boolean isDataPointsRendered;
	private final float rangeMin;
	private final float rangeMax;
    
     {
        // set a theme using the new shadow generator feature available in
        // 1.0.14 - for backwards compatibility it is not enabled by default
        ChartFactory.setChartTheme(new StandardChartTheme("JFree/Shadow",
                true));
    }

    public LineChartDrawer(List<? extends SeriesInfo> seriesInfo, 
    		String title, String xAxisLabel, String yAxisLAbel, 
    		boolean isDataPointsRendered, float rangeMin, float rangeMax) 
    {
    	this.seriesInfo = seriesInfo;
		this.title = title;
		this.xAxisLabel = xAxisLabel;
		this.yAxisLAbel = yAxisLAbel;
		this.isDataPointsRendered = isDataPointsRendered;
		this.rangeMin = rangeMin;
		this.rangeMax = rangeMax;
		
		series = new XYSeriesCollection();
    	for(SeriesInfo serie : seriesInfo){
    		series.addSeries(new XYSeries(serie.getName()));
    	}
    	
    	createChart();
	}

    private void createChart() {

        chart = ChartFactory.createXYLineChart(
            title,  // title
            xAxisLabel,             // x-axis label
            yAxisLAbel,   // y-axis label
            series,            // data
            PlotOrientation.VERTICAL,
            true,               // create legend?
            true,               // generate tooltips?
            false               // generate URLs?
        );

        chart.setBackgroundPaint(Color.white);
        chart.removeLegend();
        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        plot.setAxisOffset(new RectangleInsets(4, 4, 4, 4));
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairVisible(true);
        
        NumberAxis range = (NumberAxis) plot.getRangeAxis();
        range.setRange(rangeMin, rangeMax);
        
        XYItemRenderer r = plot.getRenderer();
        if (r instanceof XYLineAndShapeRenderer) {
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
            //renderer.setBaseShapesVisible(true);
            //renderer.setBaseShapesFilled(true);
            renderer.setDrawSeriesLineAsPath(true);
            
        	for(int i = 0; i < seriesInfo.size(); i++){
        		renderer.setSeriesPaint(i, seriesInfo.get(i).getPaint());
        	}
            
        }

    }

    public void addData(List<Double> values){
    	
    	assert values.size() == seriesInfo.size();
    	
    	for(int i = 0; i < values.size(); i++){
    		series.getSeries(seriesInfo.get(i).getName()).add( xCount, values.get(i));
    	}
    	xCount++;
    }
    
    public JPanel createDemoPanel() {
        ChartPanel panel = new ChartPanel(chart);
        panel.setFillZoomRectangle(true);
        panel.setMouseWheelEnabled(true);
        return panel;
    }

}
